package ntd.util.frame.action.com;

import cn.hutool.core.lang.Dict;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONObject;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInBo;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.util.ai.enums.EnumResType;
import ntd.util.frame.action.FactoryAction;
import ntd.util.frame.auth.ToolLogin;

public class ActionComResourceCreate extends ActionAbs {

    @Override
    public ActionInfo info() {
        return new ActionInfo("用户资源创建", "action-account-com-resource-create", this);
    }

    @Override
    public ActionOutBo handle() {
        // 分配资源 - 给创建人
        Entity entityAccountMenu = Entity.create("account_menu");
        // 传入数据
        Dict bodyObj = actionInBo.getData();

        if ("resource".equals(bodyObj.get("type"))) {
            // 创建资源
            Entity entity = new Entity("common_resource");
            entity.set("name", bodyObj.get("name"));
            entity.set("content", bodyObj.get("content"));
            entity.set("icon", bodyObj.get("icon"));
            entity.set("enum_res_type", EnumResType.APP.name());
            Entity entity1 = FactoryAction.dataSync(new ActionInBo().setDataCode("common_resource").setData(entity));
            entityAccountMenu.set("resource_id", entity1.getStr("id"));
        }

        entityAccountMenu.set("name", bodyObj.get("name"));
        entityAccountMenu.set("icon", bodyObj.get("icon"));
        entityAccountMenu.set("account_id", ToolLogin.getCurrUserId());
        entityAccountMenu.set("pid", bodyObj.get("pid"));
        FactoryAction.dataSync(new ActionInBo().setDataCode("account_menu").setData(entityAccountMenu));

        return new ActionOutBo();
    }
}
